/*
    * Author:   Sander
    * Date:     11-mei-2011
    * Name:     MTLParser
    * Version:  1.0
    *
    * This code is property of Sander blahblahblah copyright blahblah.
*/


package javaraytracer.parsers;

import javaraytracer.Materials.Image;
import java.util.ArrayList;
import javaraytracer.Vec3;

//PENDING:add function/property description
public class MTLParser
{

	public static ArrayList<MaterialLibrary> parse(String[] input, String path)
	{
		ArrayList<MaterialLibrary> materials = new ArrayList();
		MaterialLibrary material = null;
		for(int i = 0; i < input.length; i++) //check keyword
		{
			if(input[i].indexOf("newmtl") == 0)
			{
				if(material != null) //put last material in list, declaring it complete
				{
					materials.add(material);
				}
				material = new MaterialLibrary();

				material.name = input[i].split(" ")[1];
			}
			else if(input[i].indexOf("	Ka") == 0) 
			{
				String[] split = input[i].split(" ");
				material.ambient = new Vec3(Double.parseDouble(split[1]), Double.parseDouble(split[2]), Double.parseDouble(split[3]));
			}
			else if(input[i].indexOf("	Kd") == 0) //diffuse color
			{
				String[] split = input[i].split(" ");
				material.diffuse = new Vec3(Double.parseDouble(split[1]), Double.parseDouble(split[2]), Double.parseDouble(split[3]));
			}
			else if(input[i].indexOf("	Ks") == 0) //specular color
			{
				String[] split = input[i].split(" ");
				material.specular = new Vec3(Double.parseDouble(split[1]), Double.parseDouble(split[2]), Double.parseDouble(split[3]));
			}
			else if(input[i].indexOf("	Tf") == 0) //Transmission Filter color
			{
				String[] split = input[i].split(" ");
				material.specular = new Vec3(Double.parseDouble(split[1]), Double.parseDouble(split[2]), Double.parseDouble(split[3]));
			}
			else if(input[i].indexOf("	Ns") == 0) //specular amount
			{
				material.specularN = (int)Double.parseDouble(input[i].split(" ")[1]);
			}
			else if(input[i].indexOf("	d") == 0)
			{
				material.dissolve = Float.parseFloat(input[i].split(" ")[1]);
			}
			else if(input[i].indexOf("	Tr") == 0)
			{
				material.dissolve = 1f - Float.parseFloat(input[i].split(" ")[1]);
			}
			else if(input[i].indexOf("	Ni") == 0)
			{
				material.refraction = Float.parseFloat(input[i].split(" ")[1]);
			}
			else if(input[i].indexOf("	illum") == 0)
			{
				material.illumination = Integer.parseInt(input[i].split(" ")[1]);
			}
			else if(input[i].indexOf("	map_Ka") == 0)
			{
				material.ambientMap = new Image(path + input[i].split(" ")[1]);
			}
			else if(input[i].indexOf("	map_Kd") == 0)
			{
				material.diffuseMap = new Image(path + input[i].split(" ")[1]);
			}
			else if(input[i].indexOf("	map_Ks") == 0)
			{
				material.specularMap = new Image(path + input[i].split(" ")[1]);
			}
			else if(input[i].indexOf("	map_Ns") == 0)
			{
				material.specularNMap = new Image(path + input[i].split(" ")[1]);
			}
			else if(input[i].indexOf("	map_d") == 0)
			{
				material.dMap = new Image(path + input[i].split(" ")[1]);
			}
			else if(input[i].indexOf("	map_bump") == 0)
			{
				material.bumpMap = new Image(path + input[i].split(" ")[1]);
			}
		}
		materials.add(material);
		
		return materials;
	}
}
